package com.edu.edu.day25_homework.thread;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

import com.edu.edu.day25_homework.cache.BitmapCache;
import com.edu.edu.day25_homework.utils.HttpUtil;

/**
 * Created by Administrator on 2016/9/2.
 */
public class loadImageTask extends AsyncTask<String,Void,Bitmap>{

    private ImageView mLogo;
    public  loadImageTask(ImageView logo){
        this.mLogo = logo;
    }
//    public interface  onGetImageListener{
//    void onGetImage(Bitmap result);
//}
//
//    public onGetImageListener mListener;
//    public  loadImageTask(onGetImageListener listener){
//        this.mListener = listener;
//    }

    @Override
    protected Bitmap doInBackground(String... params) {
        byte[] data = HttpUtil.loadData(params[0]);
        if(data != null){
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
          //进行缓存
           if(bitmap != null){
               //将图片放在缓存中
               BitmapCache.getInstance().put(params[0],bitmap);
           }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
//        if(bitmap != null && mListener != null){
//            mListener.onGetImage(bitmap);
//        }
        if(bitmap != null){
            mLogo.setImageBitmap(bitmap);
        }
    }
}
